package org.example.everyday;

import java.util.*;

public class Lc49 {

    public static void main(String[] args) {
        Lc49 lc49 = new Lc49();
        String[]  strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = lc49.groupAnagrams(strs);
        for (List<String> list : result){
            for (String str:list){
                System.out.println(str);
            }
        }
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<String,List<String>>();
        for(String str : strs){
            char[] array = str.toCharArray();
            Arrays.sort(array);

            String key = new String(array);

            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());

    }
}
